package com.blog.controller;

import com.blog.entity.RestBean;
import com.blog.entity.article.Article;
import com.blog.service.ArticleService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/article")
public class ArticleController {

    @Resource
    private ArticleService service;

    @GetMapping("/audit")
    public RestBean<List<Article>> audit(String audit) {
        return RestBean.success(service.getArticlesByAudit(audit));
    }

    @GetMapping("/tid")
    public RestBean<List<Article>> tid(Integer tid) {
        return RestBean.success(service.getArticleByTid(tid));
    }

    @GetMapping("/uid")
    public RestBean<List<Article>> uid(Integer uid) {
        return RestBean.success(service.getArticleByUid(uid));
    }

    @GetMapping("/aid")
    public RestBean<Article> aid(Integer aid) {
        return RestBean.success(service.getArticleByAid(aid));
    }

    @PostMapping("/add")
    public RestBean<String> add(Article article) {
        String s = service.addArticle(article);
        return s == null ? RestBean.success("文章发布成功，请等待审核") : RestBean.failure(400, s);
    }

    @PostMapping("/update")
    public RestBean<String> update(Article article) {
        String s = service.updateArticle(article);
        return s == null ? RestBean.success("文章修改成功，请等待审核") : RestBean.failure(400, s);
    }

    @PostMapping("/update-audit")
    public RestBean<String> updateAudit(Article article) {
        Integer i = service.updateArticleAudit(article);
        if (i == 1) {
            return RestBean.success("文章审核成功，通过");
        } else if (i == 2) {
            return RestBean.success("文章审核成功，未通过");
        } else {
            return RestBean.failure(400, "文章审核失败");
        }
    }

    @PostMapping("/update-view")
    public RestBean<String> updateView(Article article) {
        String s = service.updateArticleView(article);
        if (s == null) {
            return RestBean.success();
        } else {
            return RestBean.failure(400, s);
        }
    }

    @PostMapping("/delete")
    public RestBean<String> delete(Integer aid) {
        String s = service.deleteArticle(aid);
        return s == null ? RestBean.success("文章删除成功") : RestBean.failure(400, s);
    }
}
